package band.wukong.practice.javase.core.thread.sync;

/**
 * Desc: 同步与并发
 * Author: sunjs
 * Date: 2009-11-6 14:20:41
 */
public class TestSyncAndConcurrency {
    public static void main(String[] args) {
        Well well = new Well();
        Person[] persons = new Person[10];
        for (int i = 1; i < 10; i++) {
            persons[i] = new Person(well);
        }
    }
}

class Person extends Thread {
    private Well well;

    public Person(Well well) {
        this.well = well;
        start();
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            well.withdraw();
            yield();
        }
    }
}

class Well {
    private int water = 1000;

    /**
     * 打一桶水
     */
    public synchronized void withdraw() {
        water--;
        System.out.println(Thread.currentThread().getName() + ": water left: " + water);
    }
}
